﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using static T.FAS.Runtime.Metadata.Entity.DataModel.DataQueryRefSchemaEntity;
using T.FAS.Runtime.Metadata.Entity;
using static T.FAS.Runtime.Metadata.Entity.DataModel.StateMachineRefSchemaEntity;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    public partial class StateMachineMetadata
    {
        public override IRefSchemaEntity BuildRefSchema()
        {
            try
            {
                var sm = this;
                StateMachineRefSchemaEntity schema = new StateMachineRefSchemaEntity
                {
                    ID = sm.ID,
                    Code = sm.Code,
                    Name = sm.Name,
                    States = new Dictionary<string, State>(),
                };
                foreach (var state in sm.States)
                {
                    schema.States[$"__{state.Key}"] = State.Mapping(sm, state);
                }
                if (sm.Events != null && sm.Events.Count > 0)
                {
                    schema.Events = new Dictionary<string, Event>();
                    foreach (var @event in sm.Events)
                    {
                        schema.Events[@event.Key] = Event.Mapping(sm, @event);
                    }
                }

                return schema;
            }
            catch (Exception e)
            {
                throw new Exception($"cdo:{Code}构造RefSchema失败, msg:{e.Message}", e);
            }
        }
    }
}
